use std::process::Stdio;

pub fn get_new_one() -> std::process::Command {
  let cmd_exe = std::process::Command::new("git");
  if cfg!(target_os = "windows") {
      let mut win_cmd = std::process::Command::new("cmd");
      win_cmd.arg("/C");
      let result_cmd = std::mem::replace(&mut win_cmd, cmd_exe);
      return result_cmd;
  }
  let mut other_shell = std::process::Command::new("bash");
  other_shell.arg("-c");
  std::mem::replace(&mut other_shell, cmd_exe)
}

pub fn run_cmd(
  cmd_text: &str,
  work_directory: &str
) {
  let mut cmd_process = get_new_one();
  println!("run cmd: [\x1B[33m{}\x1B[0m]", cmd_text);
  cmd_process.arg(cmd_text)
    .stdin(Stdio::inherit())
    .stdout(Stdio::inherit())
    .stderr(Stdio::inherit());
  if !work_directory.is_empty(){
    println!("work directory: [\x1B[33m{}\x1B[0m]", work_directory);
    cmd_process.current_dir(work_directory);
  }
  let result = cmd_process.output();
  let output_message = result.expect("运行出错");
  let msg = String::from_utf8(output_message.stdout).expect("utf8转译出错");
  println!("{}", msg);
}
